package com.teamelite.exmgmt;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.googlecode.android.widgets.DateSlider.DateSlider;
import com.googlecode.android.widgets.DateSlider.MonthYearDateSlider;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class cSummary extends Activity implements ViewFactory,OnClickListener{
	public String id_pengguna,year,month;
	ArrayList<arrList> lstTips;
	int tips_count,last_tipsid;
	TextSwitcher txtInfo;
	static final int MONTHYEARDATESELECTOR_ID = 3;
	private cFunction f = new cFunction();
	
	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_csummary);
		((Button)findViewById(R.id.btnViewDetail)).setOnClickListener(this);
		((Button)findViewById(R.id.btnClearAll)).setOnClickListener(this);
		((ImageView)findViewById(R.id.btnChDate)).setOnClickListener(this);
		
		// Get System Date Untuk Default Summary
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");			
		String datesum = sdf.format(new Date());
				
		this.year = datesum.split("-")[0];
		this.month= datesum.split("-")[1];
		
		this.id_pengguna = getIntent().getExtras().getString("id_pengguna");
		
		DBHelper db = new DBHelper(this);
		String[] data = db.getSummary(this.id_pengguna,this.year,this.month);
		
		((TextView)findViewById(R.id.tvTD)).setText(data[0]);
		((TextView)findViewById(R.id.tvCB)).setText(String.format("%.2f cal", Double.parseDouble(data[1])));
		((TextView)findViewById(R.id.tvSummaryTitle)).setText("Summary\n"+f.convertMonthToName(Integer.parseInt(this.month))+", "+this.year);
		
		// SET TIPS
		this.tips_count = db.getCountTips();
		this.last_tipsid = -1;
		lstTips = db.getTips();
		txtInfo = (TextSwitcher) findViewById(R.id.txtInfo);
		txtInfo.setFactory(this);
        Animation in = AnimationUtils.loadAnimation(this, R.anim.push_up_in);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.push_up_out);
        txtInfo.setInAnimation(in);
        txtInfo.setOutAnimation(out);
        Timer timer = new Timer();
		timer.schedule(new tTask(), 0,10000);
	}
	
	private DateSlider.OnDateSetListener mMonthYearSetListener =
	        new DateSlider.OnDateSetListener() {
	            public void onDateSet(DateSlider view, Calendar selectedDate) {
	                // update the dateText view with the corresponding date
	                cSummary.this.month=String.format("%tm", selectedDate);
	                cSummary.this.year=String.format("%tY", selectedDate);
	                DBHelper db = new DBHelper(cSummary.this);
	        		String[] data = db.getSummary(cSummary.this.id_pengguna,cSummary.this.year,cSummary.this.month);
	        		
	        		((TextView)findViewById(R.id.tvTD)).setText(data[0]);
	        		((TextView)findViewById(R.id.tvCB)).setText(String.format("%.2f cal", Double.parseDouble(data[1])));
	        		((TextView)findViewById(R.id.tvSummaryTitle)).setText("Summary\n"+f.convertMonthToName(Integer.parseInt(cSummary.this.month))+", "+cSummary.this.year);
	        		
	            }
	    };
	
	protected Dialog onCreateDialog(int id) {
		// this method is called after invoking 'showDialog' for the first time
        // here we initiate the corresponding DateSlideSelector and return the dialog to its caller
		final Calendar c = Calendar.getInstance();
		switch (id) {
            case MONTHYEARDATESELECTOR_ID:
	        return new MonthYearDateSlider(this,mMonthYearSetListener,c);
        }
        return null;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DBHelper db = new DBHelper(this);
		String[] data = db.getSummary(this.id_pengguna,this.year,this.month);
		
		((TextView)findViewById(R.id.tvTD)).setText(data[0]);
		((TextView)findViewById(R.id.tvCB)).setText(String.format("%.2f cal", Double.parseDouble(data[1])));
		((TextView)findViewById(R.id.tvSummaryTitle)).setText("Summary\n"+f.convertMonthToName(Integer.parseInt(this.month))+", "+this.year);
		
	}

	class tTask extends TimerTask {
	     @Override
	     public void run() {
	         cSummary.this.runOnUiThread(new Runnable() {
	             @Override
	             public void run() {
	            	 Random generate = new Random();
	               	 int r;
	                 do{
	                	 r = generate.nextInt(tips_count);
	                 }while(r==last_tipsid);
	                 last_tipsid = r;
	                 txtInfo.setText(lstTips.get(r).tips);
	             }
	         });
	     }
	}

	@Override
	public View makeView() {
	   TextView t = new TextView(this);
       t.setGravity(Gravity.TOP | Gravity.LEFT);
       t.setTextSize(14);
       return t;
	}
	
	public AlertDialog myAlertDialog;
	
	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
			case R.id.btnChDate:
				showDialog(MONTHYEARDATESELECTOR_ID);
			break;
			case R.id.btnViewDetail:
				DBHelper db = new DBHelper(this);
				if (db.getCountActivitySumDetail(this.id_pengguna, this.year, this.month)>0){
					Intent intent = new Intent(this, cSummaryDetail.class);
					intent.putExtra("year", this.year);
					intent.putExtra("month", this.month);
					intent.putExtra("id_pengguna", this.id_pengguna);
					startActivity(intent);
				}else{
					Toast.makeText(this, "You Can't View Detail on This Date.\nNo Data Available", Toast.LENGTH_SHORT).show();
				}
			break;
			case R.id.btnClearAll:
				AlertDialog.Builder builder = new AlertDialog.Builder(cSummary.this);
	            builder.setTitle("Confirmation");
	            builder.setMessage("You Are About To Clear All Activity Data. This Action Will Erase All of Your Saved Activites. Are You Sure?");
	            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do Nothing
					}
				});
	            builder.setPositiveButton("Yes, It's Ok", new DialogInterface.OnClickListener() {
	            	@Override
	                public void onClick(DialogInterface dialog, int arg1) {
	    				DBHelper db = new DBHelper(cSummary.this);
	    				db.delActivityByPYM(cSummary.this.id_pengguna,cSummary.this.year,cSummary.this.month);
	    				
	    				String data[] = db.getSummary(cSummary.this.id_pengguna,cSummary.this.year,cSummary.this.month);
	    				((TextView)findViewById(R.id.tvTD)).setText(data[0]);
	    				((TextView)findViewById(R.id.tvCB)).setText(String.format("%.2f cal", Double.parseDouble(data[1])));
	    				((TextView)findViewById(R.id.tvSummaryTitle)).setText("Summary\n"+f.convertMonthToName(Integer.parseInt(cSummary.this.month))+", "+cSummary.this.year);
	    				
	            	}
	            });
	            builder.setCancelable(false);
	            myAlertDialog = builder.create();
	            myAlertDialog.show();
			break;
		}
	}
}
